<?php
// $Id$

/**
 * Controller_Admin_Web 控制器
 */
class Controller_Admin_question extends Controller_Abstract {
	//科目 
	function actionIndex(){
		$this->union_table = paperquestion::meta();
		//获取搜索参数
		$this->get_param();
		//初始化分页信息
		$this->_format_page_info();
		
		//关联表
		//$this->union_table = $union_table;
		$append_data = array("acct_id"=>$_SESSION['acct']['id']);
		if (!is_null($append_data))$this->param = array_merge($this->param,$append_data);
		
		$dis_param = new helper_disposeparam($this->param);
		$sql = $dis_param->getsql();//OUT PUT用
		$_SESSION['lastsql'] = $sql;//OUT PUT用
		$obj = call_user_func_array(array($this->union_table,'find'), $sql);
		$item = $obj->limitPage($this->curr_page,$this->page_size)
					->order(' question_number asc ')
					->fetchPagination($this->pagination)
					->getall();
		$this->_view['item'] = $item;
		$this->_view['pagination'] = $this->pagination;
		$this->_view['orderField'] = $this->orderkey;
		$this->_view['orderDirection'] = $this->ordertype;
		$this->_view ['paper_id'] = $this->_context->paper_id;

	}
	function actionAdd(){
		if($this->_context->isGET()){
	    	if(!empty($this->_context->id)){
	    		$question = paperquestion::find('id=?',$this->_context->id)->getOne();
	    		$this->_view['question'] = $question;
	    		
	    	}else{
	    		$this->_view['paper_id'] = $this->_context->paper_id;
	    	}
	    	

	    }
		if($this->_context->isPOST())
		{
			if(!empty($this->_context->id)){
				$question = paperquestion::find('id=?',$this->_context->id)->getOne();
				$question->title=trim($this->_context->title);
				$question->optiona=trim($this->_context->optiona);
				$question->optionb=trim($this->_context->optionb);
				$question->optionc=trim($this->_context->optionc);
				$question->optiond=trim($this->_context->optiond);
				$question->optione=trim($this->_context->optione);
				$question->resolution=trim($this->_context->resolution);
				$question->answer=paperquestion::formatanswer($this->_context->answer);
				$question->kaodian = str_replace("，",",",trim($this->_context->kaodian));
		        $question->save(0,'update');
			}else{
				$question = new paperquestion;
				$question->title=trim($this->_context->title);
				$question->acct_id=$_SESSION['acct']['id'];
				$paper_id = intval($this->_context->paper_id);
				$question->paper_id=$paper_id;
				$question->question_number=paperquestion::find(array("paper_id"=>$paper_id))->getCount()+1;
				$question->type = paper::getpapertype($paper_id);
				$question->optiona=trim($this->_context->optiona);
				$question->optionb=trim($this->_context->optionb);
				$question->optionc=trim($this->_context->optionc);
				$question->optiond=trim($this->_context->optiond);
				$question->optione=trim($this->_context->optione);
				$question->resolution=trim($this->_context->resolution);
				$question->answer=paperquestion::formatanswer($this->_context->answer);
				$question->kaodian = str_replace("，",",",trim($this->_context->kaodian));
				$question->save(0,'create');
			}

			if(!$_POST['id'])	{
	            Syslog::logs ( '添加题目,id :' . $question->id );
	        }else{
	        	
		        Syslog::logs ( '编辑题目,id :' . $question->id );
		    }
	        $this->_context->forwardUrl = url("question")."/index/paper_id/".$question->paper_id;
			$this->success('编辑成功');
		}

	}
	function actionDel(){
		Syslog::logs("删除题目，ID：".print_r($this->_context->ids,true));
	     $this->union_table = paperquestion::meta();
	     $this->delete(array('id'=>$this->_context->ids));
	}
	//导入试题
	function actionimport(){
		$allowExt = 'xls,csv,xlsx';
		$upFolder = 'uploadfiles/tmp/';
		if($this->_context->isPOST())
		{
			$errorString  = '';
			# 图片上传
			$uploader = new Helper_Uploader();
			if ($uploader->filesCount()){
				$uploader = new Helper_Uploader();
				//var_dump($_REQUEST);
				$upfile = $uploader->file('filedata');
				$extname = $upfile->extname();# 扩展名
				$old_name = $upfile->filename();
				if($upfile->isSuccessed()){
					if($upfile->isValid($allowExt)){
						$filename = md5($old_name.time()).'.'.$extname;
						$filepath = $upFolder.$filename;
						# 移动文件
						$upfile->move($filepath);
						if (!$upfile->isMoved()){
							$errorString = '上传失败!请检查服务器配置!';
						}else{
							$errorString = '上传成功!';
							$res['filepath'] = $filepath;
							//导入试题
							paperquestion::importquestion($filepath,$this->_context->paper_id);

						}
					}else{
						$errorString = '上传失败!格式不正确!';
					}
				}else{
					$errorString = '上传失败!请检查服务器配置!';
				}
			}
			//var_dump($errorString);
			unset($uploader);

			$res['err'] = $errorString;
			echo(json_encode($res));
			
		}
	}
}